package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.card.EntityScaleUpgrade;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoScaleUpgrade;
import com.ocom.payconfig.service.ScaleUpgradeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class ScaleUpgradeServiceImpl extends ServiceImpl<IDaoScaleUpgrade, EntityScaleUpgrade> implements ScaleUpgradeService {

    @Autowired
    private IDaoScaleUpgrade dao;

    @Override
    @DataSourceAspect
    public PageInfo<EntityScaleUpgrade> getUpgradeCodeList(Date time, String upgradeCode, String description, int page, int size) {
        PageHelper.startPage(page, size);

        QueryWrapper<EntityScaleUpgrade> queryWrapper = Wrappers.query();

        queryWrapper.and(wrapper -> {
            wrapper.le("status", 2).or().le("end_time", time);
        });

        if (StringUtils.isNotBlank(upgradeCode)) {
            queryWrapper.like("upgrade_code", upgradeCode);
        }
        if (StringUtils.isNotBlank(description )) {
            queryWrapper.like("description ", description );
        }
        queryWrapper.orderByAsc( "status","end_time", "begin_time");

        PageInfo<EntityScaleUpgrade> pageInfo = new PageInfo<>(this.list(queryWrapper));
        return pageInfo;
    }

    @Override
    @DataSourceAspect
    public EntityScaleUpgrade findByUpgradeCode(String upgradeCode,String msVer) {
        QueryWrapper<EntityScaleUpgrade> queryWrapper = Wrappers.query();
        queryWrapper.eq("upgrade_code", upgradeCode);
//        queryWrapper.eq("ms_ver", msVer);
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect
    public boolean save(EntityScaleUpgrade entity) {
        return super.save(entity);
    }

    @Override
    @DataSourceAspect
    public boolean update(EntityScaleUpgrade entity) {
        return super.updateById(entity);
    }
}
